import React from 'react';
import {Table, Row, Col, Form, Radio, Select, Button, Popconfirm, message} from 'antd';
const R = require('ramda');
import AddLinkman from '../../../components/order/components/AddLinkman'
import RightBox from "../../commons/RightBox";
import RightConstants from "../../../utils/RightConstants";

const FormItem = Form.Item;

export default Form.create({wrappedComponentRef: true})(class Linkman extends React.Component {
    constructor(props) {
        super(props);
        let dataSource = R.isNil(this.props.dataSource) ? {} : this.props.dataSource
        this.state = {

        };
    }

    handleResetFields = () => {
        this.props.form.resetFields();
    }

    handleOpenAdd = () => {
        this.addLInkman.handleOpen("add");
    }


    onEdit = (record, type) => {
        this.props.dispatch({type: 'customerManage/getLinkmanById', payload: {id: record.id}});
        this.addLInkman.handleOpen(type);
    };

    columns = [
        {title: '姓名', dataIndex: 'name', key: 'name'},
        {title: '性别', render(record) {
            if(R.isNil(record.sex) || R.isEmpty(record.sex)){
                return "未选择";
            }else if(record.sex == "1"){
                return "男";
            }else if(record.sex == "2"){
                return "女";
            }else if(record.sex == "2"){
                return "未知";
            }
        }},
        {title: '手机', dataIndex: 'mobile', key: 'mobile'},
        {title: '邮箱', dataIndex: 'email', key: 'email'},
        {title: '电话', dataIndex: 'telephone', key: 'telephone'},
        {title: '职位', dataIndex: 'position', key: 'position'},
        {title: '创建时间', dataIndex: 'createtime', key: 'createtime'},
        {title: '操作', key: 'key_oprator',
            render: (text, record, index) => {
                return (
                    <span>

                        {this.props.disabled == true ?
                            <span>
                                 <a href="#" onClick={this.onEdit.bind(this, record, "view")}>查看</a>
                            </span>
                            :
                            <RightBox resource={RightConstants.SUPPLIER.SUPPLIER_MANAGE_LINKMAN_DELETE}><span>
                                <Popconfirm placement="leftTop" title={'数据删除后将不可以恢复，是否继续删除？'}
                                 onConfirm={() => {
                                 this.props.dispatch({
                                         type: 'customerManage/deleteLinkmanById',
                                         payload: {
                                          id: record.id,
                                          cstid: record.cstid,
                                         }
                                     })
                                 }} okText="是" cancelText="否">
                                 <a href="#" disabled={this.props.disabled} >删除</a>
                             </Popconfirm>
                         </span></RightBox>
                        }

                       </span>
                     );
               }
         }];

    render() {
        const { getFieldDecorator } = this.props.form;
        let dataSource = R.isNil(this.props.dataSource) ? {} : this.props.dataSource;
        let formItemLayout = {
            labelCol: { xs: { span: 24 }, sm: { span: 6 } },
            wrapperCol: { xs: { span: 24 }, sm: { span: 14 } }
        };
        let values = this.props.form.getFieldsValue();
        let pagination = {
            showTotal: total => `共 ${total} 条数据`,
            pageSize: this.props.linkmanPage.size,
            total: this.props.linkmanPage.totalRow,
            showSizeChanger: true,
            current: this.props.linkmanPage.page,
            onShowSizeChange: (current, size) => {
                let newPage = this.props.linkmanPage;
                newPage.size = size;
                newPage.page = 1;
                this.props.dispatch({
                    type: 'customerManage/getLinkmanList',
                    payload: {
                        id: R.isNil(dataSource.id) ? "" : dataSource.id,
                        page: newPage,
                        cstId: this.props.cstid
                    }
                });
            },
            onChange: (pageNumber, pageSize) => {
                let newPage = this.props.linkmanPage;
                newPage.page = pageNumber;
                this.props.dispatch({
                    type: 'customerManage/getLinkmanList',
                    payload: {
                        id: R.isNil(dataSource.id) ? "" : dataSource.id,
                        page: newPage,
                        cstId: this.props.cstid
                    }
                });
            }
        }

        return (
            <div>
                <Form>
                    <Row>
                        <Col span={12}>
                            <FormItem>
                                <RightBox resource={RightConstants.SUPPLIER.SUPPLIER_MANAGE_LINKMAN_ADD}><Button type="primary" icon="plus" size={'default'} disabled={this.props.disabled} onClick={this.handleOpenAdd.bind(this)} style={{display: this.props.disabled ? 'none' : 'inline-block' }}>111增加联系人</Button></RightBox>

                            </FormItem>
                        </Col>
                    </Row>
                </Form>
                <Table dataSource={this.props.linkmanList} columns={this.columns} bordered={true} size={'middle'} pagination={pagination} scroll={{x:true}}/>
                <AddLinkman wrappedComponentRef={(inst) => this.addLInkman = inst} dispatch={this.props.dispatch} dataSource={this.props.linkman} cstId={this.props.cstid}/>

            </div>
        )
    }
})


// WEBPACK FOOTER //
// ./public/src/components/supplier/components/Linkman.js